<template>
  <div>
    <div class="d_header">
      <div class="d_header_left">
        <a @click="goback" href="javascript:;">
          <img class="typeCake" src="../assets/images/left_go.png" alt="" />
        </a>
        <a @click="goback" href="javascript:;" class="fanhui">返回</a>
      </div>

      <div class="d_header_middle">注册</div>
    </div>
    <div class="form">
      <van-form validate-first @submit="checkForm">
        <!-- 通过 validator 进行函数校验 -->
        <van-field
          label="账号："
          v-model="uname"
          name="validator_uname"
          placeholder="请输入账号"
          :rules="[
            {
              validator: validator_uname,
              message: '输入错误！请输入正确的手机号',
            },
          ]"
        />
        <!-- 通过 validator 进行函数校验 -->
        <van-field
          label="密码："
          type="password"
          v-model="upwd"
          name="validator_upwd"
          placeholder="请输入密码"
          :rules="[{ validator: validator_upwd, message: '输入错误！6位数字' }]"
        />
        <van-field
          label="昵称："
          v-model="unickname"
          name="validator_unickname"
          placeholder="请输入昵称"
          :rules="[
            {
              validator: validator_unickname,
              message: '输入错误！2-20位数字、字母、汉字',
            },
          ]"
        />
        <!-- 通过 validator 进行异步函数校验 -->
        <div style="margin: 16px">
          <van-button
            color="#ffd100"
            round
            block
            type="info"
            native-type="submit"
            >注册</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      uname: "",

      upwd: "",

      unickname: "",
    };
  },
  mounted() {
    // this.$toast.fail('提示文案');
  },
  methods: {
    checkForm() {
      // console.log("验证成功");
      console.log("验证成功开始注册...");
      let uname = this.uname;
      let upwd = this.upwd;
      let unickname = this.unickname;
      this.axios
        .post(
          "/user/register",
          `uname=${uname}&unickname=${unickname}&upwd=${upwd}`
        )
        .then((res) => {
          console.log(res);
          if (res.data.code == 200) {
            this.$toast({
              message: "注册成功！",
              forbidClick: true,
              type: "success",
              duration: 3000,
            });
            const timer = setTimeout(() => {
              // 某些定时器操作
              console.log(666);
              this.$router.push("/login");
            }, 3000);
            // 通过$once来监听定时器，在beforeDestroy钩子可以被清除。
            this.$once("hook:beforeDestroy", () => {
              clearInterval(timer);
            });
          } else if (res.data.code == 201) {
            this.$toast({
              message: "该手机号已注册！",
              forbidClick: true,
              type: "fail",
              duration: 3000,
            });
          } else if (res.data.code == 202) {
            this.$toast({
              message: "该昵称已被占用！",
              forbidClick: true,
              type: "fail",
              duration: 3000,
            });
          }
        });
    },
    goback() {
      this.$router.go(-1);
    },
    // 校验函数返回 true 表示校验通过，false 表示不通过

    // 验证账号
    validator_uname(val) {
      return /^1[3-9]\d{9}$/.test(val);
    },
    // 验证密码
    validator_upwd(val) {
      return /^[0-9]{6}$/.test(val);
    },
    validator_unickname(val) {
      return /^[\u4E00-\u9FA5A-Za-z0-9]{2,20}$/.test(val);
    },
  },
};
</script>

<style scoped src="../assets/css/register.css"></style>
<style lang="scss" scoped></style>
